package cn.edu.gdut.csq.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.edu.gdut.csq.entity.vo.Setting;
import cn.edu.gdut.csq.service.SettingsService;
import cn.edu.gdut.csq.util.JsonUtils;
import cn.edu.gdut.csq.util.SystemScript;

@Controller
public class SettingsController {

	@Autowired
	private SettingsService settingsService;

	@RequestMapping("/settings")
	public String showSettings(HttpServletRequest req, HttpServletResponse res) {

		Setting settings = settingsService.getSettings();
		req.getSession().setAttribute("keywords", settings.getStrKeywords());
		
		req.getSession().setAttribute("keysize", settings.getkeyWordssSize());
		// req.getSession().setAttribute("citys", citys);
		
		
		return "monitor/settings";
	}

	@RequestMapping("/settings/update")
	@ResponseBody
	public String changeSettings(HttpServletRequest req,
			HttpServletResponse res, String citys, String keywords) {

		System.out.println(keywords);
		System.out.print(citys);
		settingsService.updateSettings(keywords, citys);
			
		return JsonUtils.objectToJson("200");

	}

	@RequestMapping("/start/all")
	@ResponseBody
	public String startSpiders(HttpServletRequest req, HttpServletResponse res) {
		System.out.println("start spider");

		String spidernames[] = { "job51Spider", "job58Spider", "zhaopinSpider" };
		String spidercmd = "";
		for (int i = 0; i < spidernames.length; i++) {
			spidercmd += " && python startup.py " + spidernames[i];
		}

		SystemScript.startSpider(spidercmd);

		return JsonUtils.objectToJson("200");
	}

}
